package com.fulihui.information.core.redis;

import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Map;
import java.util.Set;

/**
 * 基础类目排序存储实现
 *
 * @author Willard Hu on 2017/1/11.
 */
@Component("redisBasicCategoryContentSortDB")
public class RedisBasicCategoryContentSortDB extends AbstractRedisContentSortDB {
    /**
     * 类目资讯排序列表存 Redis 的 Key 的前缀
     */
    private static final String CATEGORY_CONTENT_ITEM_SORTED_KEY_PREFIX = "INFORMATION:CATEGORY_ITEM_DB:";

    @Override
    public long save(String contentId, double score, String categoryCode) {
        return doSave(key(categoryCode), contentId, score);
    }

    @Override
    public long bathSave(Map<String, Double> params, String categoryCode) {
        return doBathSave(key(categoryCode), params);
    }

    @Override
    public double incrScore(String contendId, double num, String categoryCode) {
        // 目前普通类目不支持修改评分的方式，评分默认使用发布时间的分钟数
        throw new UnsupportedOperationException("Unsupported incrScore");
    }

    @Override
    public Set<String> range(int start, int end, String categoryCode) {
        return doRange(key(categoryCode), start, end);
    }

    @Override
    public Set<String> rangeByScore(double min, double max, String categoryCode) {
        return doRangeByScore(key(categoryCode), min, max);
    }

    @Override
    public long rangeByScoreCount(double min, double max, String categoryCode) {
        return doRangeByScoreCount(key(categoryCode), min, max);
    }

    @Override
    public long size(String categoryCode) {
        return doSize(key(categoryCode));
    }

    @Override
    public long remove(String categoryCode, String... ids) {
        return doRemove(key(categoryCode), ids);
    }

    @Override
    public long removeRange(int start, int end, String categoryCode) {
        return doRemoveRange(key(categoryCode), start, end);
    }

    private String key(String categoryCode) {
        Assert.hasLength(categoryCode); // 类目不能为空
        return CATEGORY_CONTENT_ITEM_SORTED_KEY_PREFIX + categoryCode;
    }
}
